package cn.biplam.front.component.view.systemManage.sysprivi.user.manage.list;

import cn.biplam.common.ajxcode.SystemCode.UserManageCode;
import cn.biplam.common.bean.systemManage.privi.UserManageBean;
import cn.biplam.common.client.DataFilter;
import cn.biplam.common.client.Operator;
import cn.biplam.common.client.OperatorResult;
import cn.biplam.common.opcode.SysManageCode.UserOptCode;
import cn.biplam.common.opcode.SysManageCode.UserRoleOptCode;
import cn.biplam.common.paramters.SysCaption;
import cn.biplam.common.paramters.TreeParam;
import cn.biplam.common.paramters.UserPriviCaption;
import cn.biplam.common.struc.SysStruc.RoleIndexStruc;
import cn.biplam.common.struc.SysStruc.UserRoleStruc;
import cn.biplam.common.struc.SysStruc.UserStruc;
import cn.biplam.common.tools.TOOLS;
import cn.biplam.common.wrapper.ErrorMessage;
import cn.biplam.common.wrapper.KvMap;
import cn.biplam.common.wrapper.UserInfo;
import cn.biplam.front.component.ComponentBase;
import cn.biplam.front.component.data.common.ButtonComp;
import cn.biplam.front.component.view.common.GenderSelect;
import cn.biplam.front.component.view.common.LanguageSelect;
import cn.biplam.front.component.view.systemManage.sysprivi.role.manage.privi.RolePriviList;
import cn.biplam.front.component.view.systemManage.sysprivi.user.UserManage;
import cn.biplam.front.html.base.HtmlWindow;
import cn.biplam.front.html.base.Const.Float_Type;
import cn.biplam.front.template.base.ButtonGroup;
import cn.biplam.front.template.base.HiddenParam;
import cn.biplam.front.template.buttons.AddButton;
import cn.biplam.front.template.buttons.EditButton;
import cn.biplam.front.template.control.FlatTable;
import cn.biplam.front.template.control.FlowTable;
import cn.biplam.front.template.control.table.TablePropertys;
import cn.biplam.front.template.control.table.TablePropertys.Col_Content_Type;
import cn.biplam.front.template.control.table.TableScript.Action_Type;
import cn.biplam.front.template.pageFrame.mdManage.MdObject;
import cn.biplam.front.template.pageFrame.mdManage.MdTable;

/**
 *  用户信息管理主界面
 * @author XiaoRuxing Dec 14, 20098:37:08 PM
 * @version 1.0
 */
public class UserBase extends ComponentBase{

	public UserBase(UserInfo user, String homeUrl, String recordId,
			KvMap<String, String> params) {
		super(user, homeUrl, recordId, params);
		this.recordId = params.getByKey(TreeParam.NODE_ID);
		if(this.recordId == null){
			this.recordId = recordId;
		}
	}

	public static final String conId=UserManage.User_Base_Info+".user";
	
	/**
	 *  获取用户信息显示主界面
	 * @param mUser    用户信息
	 * @param sHomeUrl 应用系统路径
	 * @return HtmlWindow 用户管理主界面页面
	 * @throws ErrorMessage 
	 */
	public HtmlWindow get() throws ErrorMessage{
		
		MdObject mmp = new MdObject(conId,recordId);
		String tabId = conId+"t";
		MdTable table = new MdTable(tabId);
		int width = 700;
		//用户基本信息
		String baseInfoPageId = tabId+"1";
		table.putObject(getUserInfoWin(mUser, sHomeUrl, recordId,baseInfoPageId,TOOLS.intToString(width)));
		table.putObjectCol(
				getUserRoleList(tabId+"2", recordId, TOOLS.intToString(width)), 
				"用户角色列表", 
				width);
		mmp.putTable(table.get());
		//操作按钮
		mmp.putOperator(getOperator(recordId, mUser,baseInfoPageId));
		return mmp.get();
	}
	
	/**
	 * @return 用户具有的权限列表 
	 * @throws ErrorMessage
	 */
	public HtmlWindow getUserRolePriviList() throws ErrorMessage{
		//获取用户角色
		UserManageBean userManage = UserManageBean.getNewInstance(mUser.getUserID());
		OperatorResult userRole = userManage.queryUserRoleById(recordId);
		if(!TOOLS.exist(userRole))
			throw new ErrorMessage(" 删除用户权限错误,用户权限不存在!");
		
		String roleId = userRole.getValue(UserRoleStruc.ROLE_ID);
		RolePriviList list = new RolePriviList();
		list.setRoleId(roleId);
		HtmlWindow win = list.get(mUser, sHomeUrl, conId);
		win.setStyleWidth(300);
		win.setStyleHeight(240);
		return win;
	}
	
	/**
	 *  
	 * @return 用户管理操作按钮
	 * @throws ErrorMessage 
	 */
	private HtmlWindow getOperator(String recordId,UserInfo mUser,String submitCId) throws ErrorMessage{
		ButtonGroup operatorButtons=new ButtonGroup("",Float_Type.Left);
		if(recordId!=null){
			//修改
			EditButton editBt=new EditButton(
					mUser,
					submitCId,
					UserManage.User_Base_Info_Edit);
			editBt.isSubmitData(true);
			operatorButtons.put(editBt.get());
			//添加用户角色
			AddButton addBt=new AddButton(
					mUser,
					submitCId,
					UserManage.User_Role_Add,
					recordId);
			addBt.setCaption(UserPriviCaption.Add_UserRole);
			operatorButtons.put(addBt.get());
		}
		return operatorButtons.get();
	}
	
	/**
	 * @return 用户基本信息界面
	 * @throws ErrorMessage 
	 */
	private HtmlWindow getUserInfoWin(UserInfo mUser,String sHomeUrl,String recordId,String pageId,String width) throws ErrorMessage{
		OperatorResult userInfo = getUserInfo(mUser.getUserID(), recordId);
		FlatTable table = new FlatTable(pageId,recordId);
		table.putTh("姓名", 0, 0);
		table.putTd(userInfo.getValue(UserStruc.USERNAME), 0, 1);
		table.putTh("性别", 1, 0);
		table.putTd(GenderSelect.getByCode(userInfo.getValue(UserStruc.SEX)), 1, 1);
		table.putTh("语言种类", 2, 0);
		table.putTd(LanguageSelect.getByCode(userInfo.getValue(UserStruc.LANG)), 2, 1);
		
		table.putTh("编号", 0, 2);
		table.putTd(userInfo.getValue(UserStruc.USERCODE), 0, 3);
		table.putTh("出生日期", 1, 2);
		table.putTd(userInfo.getValue(UserStruc.BIRTHEDAY), 1, 3);
		table.putTh("机器名称", 2, 2);
		table.putTd(userInfo.getValue(UserStruc.USER_MACHINE), 2, 3);
		//放入附加参数
		table.putExtend(
				new HiddenParam(TreeParam.NODE_CONTARINER_ID, 
				params.getByKey(TreeParam.NODE_CONTARINER_ID)).get());
		table.setWidth(width);
		return table.get();
	}
	
	/**
	 *  获取用户角色列表
	 * @param pageId
	 * @return 用户角色列表
	 * @throws ErrorMessage 
	 */
	private HtmlWindow getUserRoleList(String pageId,String recordId,String width) throws ErrorMessage{
		TablePropertys propertys = new TablePropertys(sHomeUrl,mUser);
		propertys.putCol(RoleIndexStruc.Role_Name,"角色名称");
		propertys.putCol(RoleIndexStruc.Role_Desc,Col_Content_Type.DESC_LONG_TEXT, "角色描述");
		propertys.putCol("operator", Col_Content_Type.BUTTON, "操作", 
				ButtonComp.get(mUser, SysCaption.Delete),
				ButtonComp.get(mUser, UserPriviCaption.Privi));
		propertys.setColAction("operator", Action_Type.POP_SUBMIT_PR, UserManageCode.User_Role_Del);
		propertys.setColAction("operator", Action_Type.POP_SUBMIT_PV, UserManageCode.User_Privi_List);
		
		FlowTable table = new FlowTable(pageId,UserRoleStruc.USER_ROLE_ID,propertys);
		DataFilter filter = new DataFilter();
		filter.put(UserRoleStruc.USER_ID,recordId);
		OperatorResult sr = Operator.get(UserRoleOptCode.Query_User_Role_Exist, mUser.getUserID(), filter);
		table.put(sr);
		table.setWidth(width);
		return table.get();
	}
	
	/**
	 * @return 单个用户基本信息
	 */
	private OperatorResult getUserInfo(String userId, String recordId) throws ErrorMessage{
		DataFilter filter = new DataFilter();
		filter.put(UserStruc.USERID, recordId);
		OperatorResult rs = Operator.get(UserOptCode.Query_User_By_Id, userId, filter);
		return rs;
	}
}
